package com.ruiben.mall.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruiben.mall.IProductService;
import com.ruiben.mall.mapper.*;
import com.ruiben.mall.pojo.*;
import com.ruiben.mall.utils.CommonJSONResult;
import com.ruiben.mall.utils.PagedResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

import static com.ruiben.mall.config.Config.BASE_URL;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ShopProductMapper shopProductMapper;

    @Autowired
    private ProductPhotoMapper productPhotoMapper;

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private ProductAttrMapper productAttrMapper;



    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ProductCategory> getProductCategory() {

        Example parentExample = new Example(ProductCategory.class);
        Example.Criteria criteria = parentExample.createCriteria();
        criteria.andEqualTo("parentId", 0);
        //一级分类列表
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(parentExample);

        for (ProductCategory category:productCategories){
            if (StringUtils.isNotBlank(category.getImagePath())){
                category.setImagePath(BASE_URL + category.getImagePath());
            }
        }


        return getChildType(productCategories);
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ProductCategory getProductCategoryById(String id) {

        return productCategoryMapper.selectByPrimaryKey(id);
    }


    /**
     * 获取父级分类
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ProductCategory> getParentProductCategory() {

        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId","0");
        List<ProductCategory> categoryList = productCategoryMapper.selectByExample(example);
        for (ProductCategory category:categoryList){
            if (StringUtils.isNotBlank(category.getImagePath())){
                category.setImagePath(BASE_URL + category.getImagePath());
            }
        }
        return categoryList;
    }

    /**
     * 获取子分类
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ProductCategory> getChildProductCategory(String parentId) {

        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",parentId);
        List<ProductCategory> categoryList = productCategoryMapper.selectByExample(example);
        for (ProductCategory category:categoryList){
            if (StringUtils.isNotBlank(category.getImagePath())){
                category.setImagePath(BASE_URL + category.getImagePath());
            }
        }
        return categoryList;
    }

    private List<ProductCategory> getChildType(List<ProductCategory> productCategories) {

        List<ProductCategory> list = new ArrayList<>();

        for (ProductCategory type : productCategories) {
            Example childExample = new Example(ProductCategory.class);
            Example.Criteria childCriteria = childExample.createCriteria();
            childCriteria.andEqualTo("parentId", type.getId());
            List<ProductCategory> childType = productCategoryMapper.selectByExample(childExample);
            if (childType != null && childType.size() > 0) {
                type.setChildren(childType);
                getChildType(childType);
            }
            list.add(type);
        }

        return list;

    }


    /**
     * 获取商品列表
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedResult getProductList(int page, int pageSize) {

        PageHelper.startPage(page, pageSize);
        List<ShopProduct> shopProducts = shopProductMapper.getProductList();
        for (ShopProduct item:shopProducts){
            item.setImagePath(BASE_URL + item.getImagePath());
        }
        PageInfo<ShopProduct> pageList = new PageInfo<>(shopProducts);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(shopProducts);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public ShopProduct getProductById(String id) {
        return shopProductMapper.selectProductDetailById(id);
//        return shopProductMapper.selectByPrimaryKey(id);
//        return new ShopProduct();
    }

    @Override
    public List<ProductPhoto> getProductImagesByProductId(String productId) {
        Example example = new Example(ProductPhoto.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
//        return productPhotoMapper.getProductImagesByProductId(productId);
        return productPhotoMapper.selectByExample(example);
    }


    @Override
    public List<ProductType> getProductTypeList() {
        return productTypeMapper.selectAll();
    }




    /**
     * 根据商品id获取该商品属性列表
     */
    @Override
    public List<ProductAttr> getProductAttrList(String productId) {
        return productAttrMapper.getProductAttrList(productId);
    }


    public List<ShopProduct> getRecommendList(){
        return shopProductMapper.getRecommendList();
    }

    @Override
    public List<ShopProduct> getHotList() {
        return shopProductMapper.getHotList();
    }


    public List<ShopProduct> getProductListByAllCateId(List<String> ids){
        return shopProductMapper.getProductListByAllCateId(ids);
    }

    @Override
    public List<ProductAttr> getSpecList(String productId){
        return productAttrMapper.getSpecList(productId);
    }

    @Override
    public List<ShopProduct> getProductListByCate(String cateId,int sort) {
//        Example example = new Example(ShopProduct.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("cateId",cateId);
        List<ShopProduct> shopProducts = shopProductMapper.getProductListByCate(cateId,sort);
        return shopProducts;
    }

    @Override
    public List<ShopProduct> getProductListByPriceSort(String cateId,int sort) {
        Example example = new Example(ShopProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cateId",cateId);
        if (sort == 1){
            example.setOrderByClause("price desc");
        } else {
            example.setOrderByClause("price asc");
        }
        return shopProductMapper.selectByExample(example);
    }

    @Override
    public List<ShopProduct> getProductListBySolesSort(String cateId,int sort) {
        Example example = new Example(ShopProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cateId",cateId);
        if (sort == 1){
            example.setOrderByClause("soles desc");
        } else {
            example.setOrderByClause("soles asc");
        }
        return shopProductMapper.selectByExample(example);
    }

    @Override
    public List<ShopProduct> getProductListByNewSort(String cateId,int sort) {
        Example example = new Example(ShopProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cateId",cateId);
        criteria.andEqualTo("isnew",1);
        criteria.andEqualTo("isdelete",0);
        return shopProductMapper.selectByExample(example);
    }

    /**
     * j获取 最新商品
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ShopProduct> getNewsGoods(){
        List<ShopProduct> shopProducts = shopProductMapper.getNewList();
        return shopProducts;
    }
}
